<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/17
 * Time: 11:09
 */

namespace app\admin\controller;


use think\Db;

class Information extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $create_start_time = input('create_start_time');
            $create_end_time = input('create_end_time');
            $user_keyword = input('user_keyword');
            $keyword = input('keyword');
            
            $where['info_del'] = 1;
            // ID/昵称
            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $user_keyword . '%'];
            }
            // 手机号/邮箱
            if(isset($keyword) && $keyword != ''){
                $where['user_phone|user_email'] = ['like', '%' . $keyword . '%'];
            }
            // 发布时间
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time != ''){
                $where['info_time'] = ['between', [$create_start_time, $create_end_time]];
            }
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time == ''){
                $where['info_time'] = ['>', $create_start_time];
            }
            if(isset($create_start_time) && $create_start_time == '' && isset($create_end_time) && $create_end_time != ''){
                $where['info_time'] = ['<', $create_end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information')
                ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information')
                ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
                ->field('info_id, user_id, user_username, user_phone, user_email, info_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('info_time asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function infoDetail(){
        $info_id = input('info_id');
        $info = Db::table('up_information')
            ->join('up_u_user', 'user_id = info_user_id', 'LEFT')
            ->where('info_id', $info_id)
            ->field('info_id, user_username, user_phone, user_email, info_content, info_images, 
            info_video, info_other_user, info_time, info_comment_num, user_id')
            ->find();
        if(!empty($info['info_images'])){
            $images = trim($info['info_images'], ',');
            $info['info_images'] = explode(',', $images);
        }else{
            $info['info_images'] = [];
        }
        // @的人
        if(!empty($info['info_other_user'])){
            $info_other_user = trim($info['info_other_user'], ',');
            $users = Db::table('up_u_user')->where('user_id', 'IN', $info_other_user)->column('user_username');
            $info['info_other_user'] = implode(',', $users);
        }
        $this->assign('info', $info);
        return $this->fetch('information/infodetail');
    }
    
    /**
     * 全部评论
     * @return mixed|\think\response\Json
     */
    public function commentList(){
        if(request()->isAjax ()){
            $keyword = input('keyword');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['comment_info_id'] = session('info_id');
            $where['comment_status'] = 1;
            
            if(isset($keyword) && $keyword != ''){
                $where['user_username|user_id'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['comment_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['comment_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['comment_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information_comment')
                ->join('up_u_user', 'comment_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information_comment')
                ->join('up_u_user', 'comment_user_id = user_id', 'left')
                ->field('comment_id, comment_info_id, comment_content, comment_other_user, 
                user_username, user_id, comment_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('comment_time desc')
                ->select();
            foreach($lists as $k => $v){
                // @的人
                if(!empty($v['comment_other_user'])){
                    $comment_other_user = trim($v['comment_other_user'], ',');
                    $users = Db::table('up_u_user')->where('user_id', 'IN', $comment_other_user)->column('user_username');
                    $lists[$k]['comment_other_user'] = implode(',', $users);
                }
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $info_id = input('info_id');
        if(isset($info_id) && $info_id != ''){
            session('info_id', $info_id);
        }
        
        return $this->fetch('information/commentlist');
    }
    
    /**
     * 回复列表
     * @return mixed|\think\response\Json
     */
    public function replyList(){
        if(request()->isAjax ()){
            $keyword = input('keyword');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['reply_status'] = 1;
            $where['reply_comment_id'] = session('comment_id');
        
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['reply_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('information_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('information_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'left')
                ->field('reply_id, reply_content, reply_time, user_username, user_id, 
                user_phone, user_email')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('reply_time desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $comment_id = input('comment_id');
        if(isset($comment_id) && $comment_id != ''){
            session('comment_id', $comment_id);
        }
        
        return $this->fetch('information/replylist');
    }
    
    /**
     * 删除动态
     * @return \think\response\Json
     */
    public function infoDel(){
        $info_id = input('id');
        $res = Db::table('up_information')->where('info_id', $info_id)->setField('info_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}